package com.dg11185.nearshop.activity.user;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.libs.network.http.client.HttpRequest.ActionListener;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.app.Utils;
import com.dg11185.nearshop.net.request.ModifyPwdRequest;
import com.dg11185.nearshop.net.response.ModifyInfoResponse;
import com.wlper.lib.uitls.LogUtils;
import com.wlper.lib.uitls.ViewUtils;

/**
 * 修改密码
 * 
 * @author wanliang527
 * @date 2014-5-29
 */
public class ModifyPasswdActivity extends SubPageActivity {

    private EditText loginNameBox, oldPasswdBox, newPasswdBox,
            confirmPasswdBox;
    private Button submitBtn;
    private String mLoginName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_user_pwd);
        setSubPageTitle("修改密码");
        mLoginName = getIntent().getStringExtra(Constants.LOGIN_NAME);
        initView();
    }

    private void initView() {
        findViewsId();
        if (mLoginName != null)
            loginNameBox.setText(mLoginName);
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSetNewPasswd(oldPasswdBox.getText().toString(), newPasswdBox
                        .getText().toString());
            }
        });
    }

    /**
     * 修改面密码
     * 
     * @param oldPasswd
     *            旧密码
     * @param newPasswd
     *            新密码
     */
    public void onSetNewPasswd(String oldPasswd, String newPasswd) {
        if (!(Utils.isPasswdValid(oldPasswd) && Utils.isPasswdValid(newPasswd))) {
            if (oldPasswd.length() < 6 || newPasswd.length() < 6) {
                ViewUtils.showToast(mActivity, "密码长度不能少于6位");
            } else {
                ViewUtils.showToast(mActivity, "密码输入不合法，请重新输入");
            }
            return;
        }
        if (!newPasswd.equals(confirmPasswdBox.getText().toString())) {
            ViewUtils.showToast(mActivity, "两次输入的新密码不一致\n请重新输入");
            return;
        }
        showLoading();
        ModifyPwdRequest req = new ModifyPwdRequest(mLoginName, oldPasswd,
                newPasswd);
        req.setActionListener(new ActionListener<ModifyInfoResponse>() {
            @Override
            public void onSuccess(ModifyInfoResponse result) {
                LogUtils.logNetReturn(Constants.modifyPwd, result.responseStr);
                String status = result.status;
                hideLoading();
                if (status.equals(ModifyInfoResponse.SUCCESS)) {
                    onModifyPwdSuccess();
                } else if (status.equals(ModifyInfoResponse.MEM_ILLEGALPWD)) {
                    ViewUtils.showToast(mActivity, "您的登录密码有误\n请重新输入");
                    oldPasswdBox.setText("");
                } else {
                    LogUtils.logNetError("修改密码失败---"+status);
                    ViewUtils.showToast(mActivity, R.string.network_error);
                }

            }

            @Override
            public void onFailure(int resultCode) {
                hideLoading();
                LogUtils.logNetError("修改密码失败---" + resultCode);
                if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                    ViewUtils.showToast(mActivity,
                            R.string.cant_connect_network);
                } else {
                    ViewUtils.showToast(mActivity, R.string.network_error);
                }
            }
        });
        NetClient.httpPost(req);
    }

    /**
     * 密码修改成功，弹出对话框
     */
    public void onModifyPwdSuccess() {
        showDialog("修改密码成功", "请重新登录！").setPositiveButton("确定",
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        returnUserCenter();
                    }
                }).setCanceledOnTouchOutside(false);
    }

    /**
     * 返回用户中心，意味着密码修改成功，需要注销用户，让用户重新登录
     */
    public void returnUserCenter() {
        hideDialog();
        setResult(RESULT_OK);
        finish();
    }

    private void findViewsId() {
        loginNameBox = (EditText) findViewById(R.id.edit_login_name);
        oldPasswdBox = (EditText) findViewById(R.id.edit_old_passswd);
        newPasswdBox = (EditText) findViewById(R.id.edit_new_passwd1);
        confirmPasswdBox = (EditText) findViewById(R.id.edit_new_passwd2);
        submitBtn = (Button) findViewById(R.id.btn_submit);
    }

}
